package com.lepu.app.fun.hospital.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.DateUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.hospital.bean.HospitalAdvice.Reply;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.app.usercenter.bean.LoginInfo;

public class HospitalAdviceDetailAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	private ArrayList<Reply> mArrayList = null;
	private ArrayList<String> mImageList = null;

	public HospitalAdviceDetailAdapter(Context context, ArrayList<Reply> arrayList, ArrayList<String> imageList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mImageList = imageList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.hospital_advice_detail_item, null);

			holder = new ViewHolder();

			RelativeLayout rightLayout = (RelativeLayout) convertView.findViewById(R.id.rightLayout);
			TextView consult_question_item_text = (TextView) convertView.findViewById(R.id.consult_question_item_text);
			TextView questionTime = (TextView) convertView.findViewById(R.id.questionTime);
			ImageView questionImage = (ImageView) convertView.findViewById(R.id.consult_question_item_image);
			CircularImageView consult_question_item_icon = (CircularImageView) convertView.findViewById(R.id.consult_question_item_icon);

			LinearLayout leftLayout = (LinearLayout) convertView.findViewById(R.id.leftLayout);
			TextView descTextView = (TextView) convertView.findViewById(R.id.descTextView);
			TextView answerTime = (TextView) convertView.findViewById(R.id.answerTime);

			holder.rightLayout = rightLayout;
			holder.consult_question_item_text = consult_question_item_text;
			holder.consult_question_item_icon = consult_question_item_icon;
			holder.questionImage = questionImage;
			holder.questionTime = questionTime;

			holder.leftLayout = leftLayout;
			holder.descTextView = descTextView;
			holder.answerTime = answerTime;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Reply item = mArrayList.get(position);

		if (!TextUtils.isEmpty(item.UserID)) {
			holder.leftLayout.setVisibility(View.GONE);
			holder.rightLayout.setVisibility(View.VISIBLE);

			if(MyApplication.getInstance().checkIsLogin())
			{
				LoginInfo loginInfo = MyApplication.getInstance().getLoginInfo();
				String imageUrl = Setting.getImageUrl(loginInfo.Avatar);
				UniversalImageLoadTool.disPlay(imageUrl, holder.consult_question_item_icon, R.drawable.default_icon_small);
			}else{
				holder.consult_question_item_icon.setImageResource(R.drawable.icon_head_nologin);
			}
			
			if(item.ContentType.equals("1") || item.ContentType.equals("")){
				holder.consult_question_item_text.setVisibility(View.VISIBLE);
				holder.questionImage.setVisibility(View.GONE);
				
				holder.consult_question_item_text.setText(item.ReplyContent);
			}else if(item.ContentType.equals("2")){
				holder.consult_question_item_text.setVisibility(View.GONE);
				holder.questionImage.setVisibility(View.VISIBLE);
				
				UniversalImageLoadTool.disPlay(item.ReplyContent, holder.questionImage, R.drawable.default_icon_big);
				
				holder.questionImage.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						String[] urlArray = new String[mImageList.size()];
                        int index = 0;
                        
						for (int i = 0; i < mImageList.size(); i++) {
							urlArray[i] = mImageList.get(i);
							
							if(item.ReplyContent.equals(mImageList.get(i))){
								index = i;
							}
						}

						
						Intent intent = new Intent(mContext,AppImageBroswerActivity.class);
						
						intent.putExtra("isShowTopbar", false);
						intent.putExtra("index", index);
						intent.putExtra("urlArray", urlArray);
						
						((BaseFragmentActivity) mContext).startActivity(intent);
						
					}
				});
			}
			
			holder.questionTime.setText(DateUtil.getHMTime(item.ReplyTime));

		} else if(!TextUtils.isEmpty(item.StaffID)){
			holder.leftLayout.setVisibility(View.VISIBLE);
			holder.rightLayout.setVisibility(View.GONE);
			
			holder.descTextView.setText(item.ReplyContent);
			holder.answerTime.setText(DateUtil.getHMTime(item.ReplyTime));
		}

		return convertView;
	}
	
	static class ViewHolder {
		RelativeLayout rightLayout;
		TextView consult_question_item_text, questionTime;
		ImageView questionImage;
		CircularImageView consult_question_item_icon;

		LinearLayout leftLayout;
		TextView descTextView, answerTime;
	}

}
